from itsdangerous import URLSafeTimedSerializer
from app_db import app


# create token
def generate_confirmation_token(email):
    serializer = URLSafeTimedSerializer(app.config['SECRET_KEY'])
    return serializer.dumps(email, salt=app.config['SECURITY_PASSWORD_SALT'])


# email with token
def confirm_token(token, epiration = 60 * 60):
    serializer = URLSafeTimedSerializer(app.config['SECRET_KEY'])
    try:
        email = serializer.loads(
            token,
            salt = app.config['SECURITY_PASSWORD_SALT'],
            max_age=epiration
        )
    except Exception:
        return False
    return email
